/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package accountproject;

import accountproject.data.DataIO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author Amon
 */
public class WorksComboModel extends DefaultComboBoxModel {

    private DataIO dataIO;
    private HashMap<String, Integer> workIdMap;//Worktext, id
    private HashMap<String, Integer> workUnitPriceMap;//Worktext, id


    public WorksComboModel() {
        super();
        readWorks();
    }

    private void readWorks() {
        try {
            if (null == dataIO) {
                dataIO = new DataIO();
            }
            removeAllElements();
            workIdMap = new HashMap<String, Integer>();
            workUnitPriceMap = new HashMap<String, Integer>();
            String listElement;
            ResultSet rs = dataIO.getWorks();
            while (rs.next()) {
                listElement = rs.getString(2);
                addElement(listElement);
                workIdMap.put(listElement, rs.getInt(1));
                workUnitPriceMap.put(listElement, rs.getInt(4));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CustomerListModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public int getWorkIndex(String workText) {
        return workIdMap.get(workText);
    }

    public int getWorkUnitPrice(String workText) {
        return workUnitPriceMap.get(workText);
    }
}
